package com.ruoyi.entity;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.domain.T09;
import io.netty.util.CharsetUtil;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

public class Ftp {
    private static final String FTP_HOST = "47.121.118.149";
    private static final int FTP_PORT = 21;
    private static final String FTP_USER = "peace";
    private static final String FTP_PASSWORD = "123456";
    private static final String FTP_UPLOAD_PATH ="/data/ftp";
    private static String LOCAL_CHARSET = "GBK";
    private static String SERVER_CHARSET = "UTF-8";
    public static FTPClient init(){
        FTPClient ftpClient = new FTPClient();
        ftpClient.setAutodetectUTF8(true);
        ftpClient.setCharset(CharsetUtil.UTF_8);
        ftpClient.setControlEncoding(CharsetUtil.UTF_8.name());
        try {
            ftpClient.connect(FTP_HOST, FTP_PORT);
            ftpClient.login(FTP_USER, FTP_PASSWORD);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.setControlEncoding("UTF-8");
        } catch (SocketException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ftpClient;
    }
    public static T09 upload(MultipartFile file, FTPClient ftpClient) throws IOException {
        T09 t09=new T09();
        if (!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            try (var inputStream = file.getInputStream()) {
                boolean success = ftpClient.storeFile(fileName, inputStream);
                if (success) {
                        // 获取文件大小
                    long fileSize = file.getSize();
                    String  filesize = String.valueOf(fileSize);
                        // 如果你希望以更友好的方式（如KB, MB）显示文件大小，可以这样做
                    if (fileSize < 1024) {
                        filesize=filesize+"字节";
                    } else if (fileSize < 1024 * 1024) {
                        filesize=String.valueOf(fileSize / 1024) + " KB";
                    } else {
                        filesize=(fileSize / (1024 * 1024)) + " MB";
                    }
                        // 获取文件类型
                    String fileFormat = fileName.substring(fileName.lastIndexOf(".") + 1);
                        // 获取时间
                    Date date = new Date();
                        // 获取字数
                    int fileWordCount = FileWordCount.countWords(file);
                        // 假设这里有一个T09对象和服务
                    t09.setF0301(1L);
                    t09.setF0902(fileName);
                    t09.setF0903(filesize);
                    t09.setF0904(fileFormat);
                    t09.setF0905(date);
                    t09.setF0906(0L);
                    t09.setF0907((long) fileWordCount);
                    t09.setF0908(fileName);
                }
            } catch (IOException e) {
                throw new IOException(e);
            }
        }
        return t09;
    }
}
